/**
 * Copyright 2007-2008 Vidar Svansson
 *     
 * This file is part of JAdapter.
 * 
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jadapter.adapters;

import org.jadapter.Adapter;

import java.lang.reflect.Type;


/**
 * Convenient superclass for adapters.
 * Most stuff implemented here is to tell the runtime
 * what types the adapter applies to.
 * This is necessary because of generic type erasure.
 *
 * <br /><br />
 * The transform method is partially implemented and delegates to the
 * doTransform template method if it fails to transform.
 * 
 * @author Vidar Svansson
 *
 * @param <T> The type this adapter transforms to.
 * @param <F> The type this adapter transforms from.
 * 
 *
 */
public abstract class AbstractAdapter<T,F> 
        implements Adapter<T, F> {

    private Type from;
    private Type to;
    
    public AbstractAdapter(Class<F> from, Class<T> to){
        this.from = from;
        this.to = to;
    }
    
    /**
     * @deprecated bad state.
     * Default constructor. Requires the type information to
     * be set using {@link #from(Type)} and {@link #to(Type)}.
     */
    public AbstractAdapter(){
        // no type information available here.
    }
    
    /**
     * @deprecated use @link AdapterBuilder
     *
     * @param from The type to adapt from.
     * @return reference to this object for chaining.
     */
    public AbstractAdapter<T, F> from(Type from) {
        this.from = from;
        return this;
    }
    
    /**
     * @deprecated use @link AdapterBuilder
     * 
     * @param to The type to adapt to.
     * @return reference to this object for chaining.
     */
    public AbstractAdapter<T, F> to(Type to){
        this.to = to;
        return this;
    }
    
    /* (non-Javadoc)
     * @see org.jadapter.func.Transformers#from()
     */
    public Type from(){ return from; }
    
    /* (non-Javadoc)
     * @see org.jadapter.func.Transformers#to()
     */
    public Type to(){ return to; }
    
    
    /* (non-Javadoc)
     * @see org.jadapter.func.Transformers#transform(F)
     */
    public T transform(F context){
        Type type = to();
        if(type instanceof Class){
            Class<?> typeClass = (Class<?>) type;
            if(typeClass.isInstance(context))
                return (T)context;


        }

        return doTransform(context, type);
    }

    /**
     * Template method for implementors.
     * @param context the context to transform
     * @param type Type to transform to
     *
     * @return an instance of T transformed from the context
     */
    protected T doTransform(F context, Type type) {
        throw new UnsupportedOperationException("Adapter template method not implemented");
    }

    /**
     * @deprecated This is some ugly hack I have to look better at in 0.5
     *
     *
     * @param type the type we want to return
     * @param result possibly an instance of the type
     * @param self another possible instance of the type
     * @return either result or self.
     */
    protected T resultOrSelf(Type type, Object result, Object self) {
        Class klass;
        if(type instanceof Class) {
            klass = (Class)type;
            return (T) (klass.isInstance(result) ? result : self);
        }
           throw new IllegalArgumentException("type must be Class");


    }
    
}
